package com.swind.fast.modules.app.halali.customer.component;

import java.net.URL;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;

import com.swind.fast.common.util.oConvertUtils;

import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.swind.fast.modules.app.halali.customer.vo.response.HomeResponse;
import com.swind.fast.modules.app.halali.customer.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantMealService;

import com.swind.fast.modules.app.halali.customer.entity.RestaurantCategory;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantCategoryService;

import com.swind.fast.modules.app.halali.customer.entity.MealCategory;
import com.swind.fast.modules.app.halali.customer.service.IMealCategoryService;
import com.swind.fast.modules.app.halali.customer.entity.RestaurantMealDeal;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantMealDealService;
import com.swind.fast.modules.app.halali.customer.entity.Restaurant;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantService;

import org.springframework.context.ApplicationContext;

import com.swind.fast.common.system.util.JwtUtil;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import com.swind.fast.common.system.vo.DictModel;
import com.swind.fast.modules.system.service.ISysDictService;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

@Slf4j
@Component
public class FrontComponent {
    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private IMealCategoryService mealCategoryService;

    @Autowired
    private IRestaurantMealDealService mealDealService;

    @Autowired
    private IRestaurantService restaurantService;

    @Autowired
    private IRestaurantCategoryService restaurantCategoryService;

    @Autowired
    private IRestaurantMealService mealService;

    @Autowired
    private ISysDictService sysDictService;

    public HomeResponse buildHomeResponse() {
        HomeResponse response = new HomeResponse();

        List<DictModel> slides = getHalaliSlides();
        response.setSlides(slides);

        List<MealCategory> categories = getRestaurantCategories();
        response.setCategories(categories);

        List<RestaurantMealDeal> deals = getRestaurantMealDeals();
        response.setMealDeals(deals);

        List<Restaurant> restaurants = getRestaurants();
        response.setRestaurants(restaurants);

        return response;
    }

    public List<DictModel> getHalaliSlides() {
        String dictCode = "halali_slide";
        List<DictModel> items = sysDictService.queryDictItemsByCodeWithoutCache(dictCode);

        if (oConvertUtils.isEmpty(items)) {
            items = new ArrayList<DictModel>();
        }
        return items;
    }

    public List<MealCategory> getRestaurantCategories() {
        // QueryWrapper<MealCategory> queryWrapper = new QueryWrapper<MealCategory>().select("id", "title", "pic");
        QueryWrapper<MealCategory> queryWrapper = new QueryWrapper<MealCategory>();
        List<MealCategory> items = mealCategoryService.list(queryWrapper);

        if (oConvertUtils.isEmpty(items)) {
            items = new ArrayList<MealCategory>();
        }
        return items;
    }

    public List<RestaurantMealDeal> getRestaurantMealDeals() {
        // QueryWrapper<RestaurantMealDeal> queryWrapper = new QueryWrapper<RestaurantMealDeal>().select("id", "title", "pic");
        QueryWrapper<RestaurantMealDeal> queryWrapper = new QueryWrapper<RestaurantMealDeal>();
        List<RestaurantMealDeal> items = mealDealService.list(queryWrapper);

        if (oConvertUtils.isEmpty(items)) {
            items = new ArrayList<RestaurantMealDeal>();
        }
        return items;
    }

    public List<Restaurant> getRestaurants() {
        // QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<Restaurant>().select("id", "title", "pic");
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<Restaurant>();
        List<Restaurant> items = restaurantService.list(queryWrapper);

        if (oConvertUtils.isEmpty(items)) {
            items = new ArrayList<Restaurant>();
        } else {
            items.stream().forEach(item -> {
                item.setDeliveryTime("30");
                item.setDeliveryDistance("2.5");
            });
        }
        return items;
    }

}